import { ApiCode, ApiResult, Page, RequestUrlEnum, ResponseLoginUserInfo } from '@/config/api_config/api_interface';
import { AxiosRequestConfig } from 'axios';
import { request } from '@/config/api_config/request';
import { ElMessage } from 'element-plus';
import { router } from '@/config/router_config';
import { RouteNameEnum } from '@/config/router_config/router_components';
import { WindUser } from '@/global';
import { WindAccount } from '@/pages/MainMenu/User/WindAccount/WindAccountList';
import { AccountSearch } from '@/pages/MainMenu/User/WindAccount/WindAccountOperate';
import { LoginUser } from '@/pages/Login/Login';
import { UserSearch } from '@/pages/MainMenu/User/WindUser/WindUserList';

/**
 * 统一的请求方法
 * @param urlName 请求地址，只支持enum
 * @param config { data: any, params: any, method: Method } 参数配置
 * @return Promise<ApiResult<T>>
 */
function apiSend<T, V> (urlName: RequestUrlEnum, config: AxiosRequestConfig) : Promise<ApiResult<T>> {
  return new Promise((resolve, reject) => {
    const req = request({
      method: config.method,
      url: urlName.toString(),
      data: config.data,
      params: config.params,
      headers: config.headers
    });
    req.then(res => {
      console.log(config.params);
      if ((res.headers['content-type']).includes('application/json')) {
        const data = res.data as ApiResult<T>;
        if (data.apiCode === ApiCode.Success) {
          resolve(data);
        } else if (data.apiCode === ApiCode.NotLogin) {
          ElMessage.error(data.msg as string);
          router.push({ name: RouteNameEnum.login }).then(() => {
            reject(data.msg);
          });
        } else {
          ElMessage.error(data.msg as string);
          resolve(data);
        }
      }
      resolve(res.data as ApiResult<T>);
    }).catch(reason => {
      ElMessage.error(reason);
      reject(reason);
    });
  });
}

/** *************  账户有关接口 *****************************/
export namespace Apis{
  /**
   * 登录接口
   * @param loginUser
   */
  export function apiAdminLogin (loginUser: LoginUser): Promise<ApiResult<ResponseLoginUserInfo>> {
    return apiSend(RequestUrlEnum.adminLogin, { data: loginUser, method: 'post' });
  }

  /**
   * 获取所有的风旅账户列表
   * @param params 要查询的风旅账户对象
   */
  export function apiGetWindAccountList (params: AccountSearch | Page): Promise<ApiResult<WindAccount[]>> {
    return apiSend(RequestUrlEnum.windAccountList, { params, method: 'get' });
  }

/** ************ 用户有关接口 ************************/
  /**
   * 获取所有的风旅用户列表
   */
  export function apiGetWindUserList (params: UserSearch | Page): Promise<ApiResult<WindUser[]>> {
    return apiSend(RequestUrlEnum.windUserList, { params, method: 'get' });
  }

  /**
   * 获取某个用户绑定的所有账户
   * @param id 要查询的风旅用户id
   */
  export function apiGetBoundUser (id: string): Promise<ApiResult<WindAccount[]>> {
    return apiSend(RequestUrlEnum.windUserBoundAccounts, { params: { id }, method: 'get' });
  }

  /**
   * 删除某个windUser用户
   * @param id 用户id
   */
  export function apiDeleteWindUser (id: string): Promise<ApiResult<void>> {
    return apiSend(RequestUrlEnum.windUserDelete, { data: id, method: 'post', headers: { 'content-type': 'text/html' } });
  }

  /**
   * 激活或者还原用户
   * @param id 用户id
   */
  export function apiActiveWindUser (id: string): Promise<ApiResult<void>> {
    return apiSend(RequestUrlEnum.windUserActive, { params: { id }, method: 'get' });
  }
}
